package com.lrkj.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lrkj.back.Const;
import com.lrkj.back.entity.TbEmployee;
import com.lrkj.back.entity.TbYijian;
import com.lrkj.back.service.TbYijianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.cbons.mamisay.parent.exception.BusinessException;

import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("yijian")
public class TbYijianController extends BaseController{
	@Autowired
	private TbYijianService yijianService;
	
	//临床科室--投诉与建议
	@RequestMapping("/yijianList.do")
	public ModelAndView yijianList(String searchTxt,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Integer pageNo = request.getParameter("pageIndex")==null? 1 :Integer.valueOf(request.getParameter("pageIndex"));
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("pageNo", pageNo);
		map.put("pageSize", Const.PAGE_SIZE);
		PageInfo<TbYijian> page = yijianService.findAllYijian(map);
		request.setAttribute("yijianList", page.getList());
		request.setAttribute("searchTxt", searchTxt==null?"":searchTxt);
		request.setAttribute("total", page.getPages());
		return new ModelAndView("/linchuang/tousujianyi/yijian_list");
	}
	//点添加和修改按纽用到的方法
	@RequestMapping("/yijianEdit.do")
	public ModelAndView yijianEdit(Integer id, HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		TbYijian yijian = (id == null) ? new TbYijian() : yijianService.selectByPrimaryKey(id);
		request.setAttribute("yijian", yijian);
		return new ModelAndView("/linchuang/tousujianyi/yijian_edit");
	}
	//修改和保存的方法
	@RequestMapping("/yijianSave.do")
	@ResponseBody
	public Map<Object,Object> yijianSave(TbYijian yijian ,HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			yijian.setRecordtime(new Date());
			if(yijian.getId() != null){
				yijianService.updateByPrimaryKey(yijian);
			}else{
				TbEmployee emp =(TbEmployee)request.getSession().getAttribute("emp");
				yijian.setEmpid(emp.getEmployeeId());
				yijian.setKeshiid(emp.getDepartmentId());
				yijianService.insert(yijian);
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "编辑成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "编辑失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
	@RequestMapping("/yijianDel.do")
	@ResponseBody
	public Map<Object,Object> packageingDel(HttpServletRequest request,
			HttpServletResponse response)throws BusinessException{
		Map<Object, Object> rtnMap = new HashMap<Object,Object>();
		try {
			String ids = request.getParameter("id");
			String[] arraId = ids.split(",");
			for(String tid : arraId){
				yijianService.deleteByPrimaryKey(Integer.valueOf(tid));
			}
			rtnMap.put("status", "true");
			rtnMap.put("msg", "删除成功");
		} catch (Exception e) {
			rtnMap.put("status", "false");
			rtnMap.put("msg", "删除失败");
			e.printStackTrace();
		}
		return rtnMap;
	}
	
}
